package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IPermissionBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.common.dto.Result;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.enums.ResultCodeEnum;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.FunctionPermissionVO;
import com.yunpeng.aidevice.common.vo.PermissionDetailVO;
import com.yunpeng.aidevice.permission.service.IPermissionOuterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * PermissionController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-11
 */

@RestController
@RequestMapping("/api/permission")
public class PermissionController {

    @Resource
    private IPermissionOuterService permissionOuterService;

    @Resource
    private IPermissionBizService permissionBizService;


    /**
     * 获取用户功能权限树
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getUsersPermission", method = RequestMethod.POST)
    public Result<List<FunctionPermissionVO>> getUsersPermission() {
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return Result.success(permissionOuterService.getUsersPermissionTree(loginUserCacheDTO.getCode()));
    }

    /**
     * 验证登录用户是否有指定功能权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkPermission", method = RequestMethod.POST)
    public Result<Boolean> checkPermission(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String nodeLocation = requestParam.getString("nodeLocation");
        ParametersCheckUtil.isNotBlank(nodeLocation, "功能路径为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        if (permissionOuterService.checkPermission(nodeLocation, loginUserCacheDTO.getCode())) {
            return Result.success(true);
        }
        return Result.build(ResultCodeEnum.HAS_NOT_PERMISSION, false);
    }

    /**
     * 新增权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<Void> addNew(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String name = requestParam.getString("name");
        ParametersCheckUtil.isNotBlank(name, "权限名称为空");
        return permissionBizService.addNew(name, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 移除权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public Result<Void> remove(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        return permissionBizService.remove(permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }


    /**
     * 服务端权限分页数据列表
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String name = requestParam.getString("name");
        Integer pageSize = requestParam.getInteger("pageSize");
        Integer currentPage = requestParam.getInteger("currentPage");
        return permissionBizService.queryList(name, pageSize, currentPage);
    }

    /**
     * 禁用权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public Result<Void> disable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return permissionBizService.disabled(permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 启用权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public Result<Void> enable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        permissionOuterService.clearWholePermissionCache(ConfigConstantValues.getLoginUserCacheDTO().getCode());
        return permissionBizService.enabled(permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }


    /**
     * 用户绑定权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "userBindPermissions", method = RequestMethod.POST)
    public Result<Void> userBindPermissions(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        String userCode = requestParam.getString("userCode");
        Date startTime = requestParam.getDate("startTime");
        Date endTime = requestParam.getDate("endTime");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        permissionOuterService.clearUserCache(userCode);
        return permissionBizService.userBindPermissions(userCode, permissionCode, startTime, endTime, ConfigConstantValues.getLoginUserCacheDTO().getCode(), ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 用户解绑权限
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "userUnbindPermissions", method = RequestMethod.POST)
    public Result<Void> userUnbindPermissions(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        String userCode = requestParam.getString("userCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        permissionOuterService.clearUserCache(userCode);
        return permissionBizService.unbindUserPermissions(userCode, permissionCode, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 权限详情
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<PermissionDetailVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String permissionCode = requestParam.getString("permissionCode");
        ParametersCheckUtil.isNotBlank(permissionCode, "权限代码为空");
        return permissionBizService.permissionDetail(permissionCode);
    }

    /**
     * 清理用户缓存
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "clearUserPermissionCache", method = RequestMethod.POST)
    public Result<Void> clearUserPermissionCache(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String userCode = requestParam.getString("userCode");
        ParametersCheckUtil.isNotBlank(userCode, "用户代码为空");
        permissionOuterService.clearUserCache(userCode);
        return Result.success();
    }
}
